package com.malbbmsomanager.management;

import com.malbbmsomanager.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class DM_BBM_Items_Information extends Activity {
	private static final String TAG = null;
	private SQLiteDatabase mydb;
	private String DBNAME = "dminvoicemanagerbbm.db";
	String CurrentRefItem;
	EditText et_dmbbm_itemmaster_a, et_dmbbm_itemmaster_b,
			et_dmbbm_itemmaster_c, et_dmbbm_itemmaster_d,
			et_dmbbm_itemmaster_e, et_dmbbm_itemmaster_f,
			et_dmbbm_itemmaster_g, et_dmbbm_itemmaster_h,
			et_dmbbm_itemmaster_i, et_dmbbm_itemmaster_j,
			et_dmbbm_itemmaster_k, et_dmbbm_itemmaster_l,
			et_dmbbm_itemmaster_m, et_dmbbm_itemmaster_n,
			et_dmbbm_itemmaster_o;

	public void btnBack(View view) {
		Intent myIntent = new Intent(view.getContext(),
				DM_BBM_Item_Master.class);
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_bbm_items_information);
		TextView txtHeading = (TextView) findViewById(R.id.txtHeading);
		txtHeading.setText("Items Information");
		et_dmbbm_itemmaster_a = (EditText) findViewById(R.id.et_dmbbm_itemmaster_a);
		et_dmbbm_itemmaster_b = (EditText) findViewById(R.id.et_dmbbm_itemmaster_b);
		et_dmbbm_itemmaster_c = (EditText) findViewById(R.id.et_dmbbm_itemmaster_c);
		et_dmbbm_itemmaster_d = (EditText) findViewById(R.id.et_dmbbm_itemmaster_d);
		et_dmbbm_itemmaster_e = (EditText) findViewById(R.id.et_dmbbm_itemmaster_e);
		et_dmbbm_itemmaster_f = (EditText) findViewById(R.id.et_dmbbm_itemmaster_f);
		et_dmbbm_itemmaster_g = (EditText) findViewById(R.id.et_dmbbm_itemmaster_g);
		et_dmbbm_itemmaster_h = (EditText) findViewById(R.id.et_dmbbm_itemmaster_h);
		et_dmbbm_itemmaster_i = (EditText) findViewById(R.id.et_dmbbm_itemmaster_i);
		et_dmbbm_itemmaster_j = (EditText) findViewById(R.id.et_dmbbm_itemmaster_j);
		et_dmbbm_itemmaster_k = (EditText) findViewById(R.id.et_dmbbm_itemmaster_k);
		et_dmbbm_itemmaster_l = (EditText) findViewById(R.id.et_dmbbm_itemmaster_l);
		et_dmbbm_itemmaster_m = (EditText) findViewById(R.id.et_dmbbm_itemmaster_m);
		et_dmbbm_itemmaster_n = (EditText) findViewById(R.id.et_dmbbm_itemmaster_n);
		et_dmbbm_itemmaster_o = (EditText) findViewById(R.id.et_dmbbm_itemmaster_o);
		selectCurrentItemCode();

	}

	private void selectCurrentItemCode() {
		// TODO Auto-generated method stub
		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor curCurrentItems = null;
		try {
			String query = "SELECT * from FItemsMasterSum";
			curCurrentItems = mydb.rawQuery(query, null);
		} catch (Exception e) {
		}
		if (curCurrentItems != null) {
			if (curCurrentItems.getCount() > 0) {
				while (curCurrentItems.moveToNext()) {
					CurrentRefItem = curCurrentItems.getString(1);
					Log.v(TAG, "CurrentRefItem XXX :" + CurrentRefItem);
					filterItemsInfomation();
				}
			}
		}
		curCurrentItems.close();
		mydb.close();
	}

	private void filterItemsInfomation() {
		// TODO Auto-generated method stub
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor chkItemdDet = null;
		try {
			String queryChkItemInfo = "SELECT fi.ItemCode,fi.ItemName,fi.PurDesc,fi.TypeCode,fi.BrandCode,fi.UnitCode,fi.PackSize,fi.ReOrderLvl,fi.packing,fi.CatCode,fi.cosacc,fi.PrilCode,fi.TaxComCode,fi.GroupCode,fb.BrandName,fb.BrandCode FROM FBrand fb,FItems fi WHERE fi.BrandCode = fb.BrandCode AND fi.ItemCode = '"
					+ CurrentRefItem + "'";
			chkItemdDet = mydb.rawQuery(queryChkItemInfo, null);
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		if (chkItemdDet != null) {
			if (chkItemdDet.getCount() > 0) {
				while (chkItemdDet.moveToNext()) {
					//String ItemCode = chkItemdDet.getString(0);
					// String strItemCode = chkItemdDet.getString(0);
					String ItemCode = chkItemdDet.getString(0);
					String ItemName = chkItemdDet.getString(1);
					String PurDesc = chkItemdDet.getString(2);
					String TypeCode = chkItemdDet.getString(3);
					String BrandCode = chkItemdDet.getString(4);
					String UnitCode = chkItemdDet.getString(5);
					String PackSize = chkItemdDet.getString(6);
					String ReOrderLvl = chkItemdDet.getString(7);
					String packing = chkItemdDet.getString(8);
					String CatCode = chkItemdDet.getString(9);
					String cosacc = chkItemdDet.getString(10);
					String PrilCode = chkItemdDet.getString(11);
					String TaxComCode = chkItemdDet.getString(12);
					String GroupCode = chkItemdDet.getString(13);
					String BrandName = chkItemdDet.getString(14);

					et_dmbbm_itemmaster_a.setText(ItemCode);
					et_dmbbm_itemmaster_b.setText(ItemName);
					et_dmbbm_itemmaster_c.setText(PurDesc);
					et_dmbbm_itemmaster_d.setText(TypeCode);
					et_dmbbm_itemmaster_e.setText(BrandCode);
					et_dmbbm_itemmaster_f.setText(UnitCode);
					et_dmbbm_itemmaster_g.setText(PackSize);
					et_dmbbm_itemmaster_h.setText(ReOrderLvl);
					et_dmbbm_itemmaster_i.setText(packing);
					et_dmbbm_itemmaster_j.setText(CatCode);
					et_dmbbm_itemmaster_k.setText(cosacc);
					et_dmbbm_itemmaster_l.setText(PrilCode);
					et_dmbbm_itemmaster_m.setText(TaxComCode);
					et_dmbbm_itemmaster_n.setText(GroupCode);
					et_dmbbm_itemmaster_o.setText(BrandName);
					Log.v(TAG, "chkRefNo XXX :" + ItemCode);
				}
			}
		}
		chkItemdDet.close();
		mydb.close();
	}

	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
